package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.ui.KeywordsView;

public class SearchPage extends Activity implements View.OnClickListener {
    private String[] totalKeys = null;
    private  String[] key_words=new String[15];
    protected static final String TAG = "SearchPage";

    private KeywordsView showKeywords = null;
    private LinearLayout searchLayout = null;

    private GestureDetector mggd;

    private boolean isOutter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.searchpage);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_search);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.main_search);

        searchLayout = (LinearLayout) this.findViewById(R.id.searchContent);

        showKeywords = (KeywordsView) this.findViewById(R.id.word);
        showKeywords.setDuration(2000l);
        showKeywords.setOnClickListener(this);
        this.mggd =new GestureDetector(new Mygdlinseter());
        showKeywords.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return mggd.onTouchEvent(event);
            }
        });

        isOutter = true;

        handler.sendEmptyMessage(MSG_START_LOAD);

    }

    private String[]getRandomArray(){
        if (totalKeys != null && totalKeys.length > 0) {
            String[] keys = new String[15];
            List<String> ks = new ArrayList<String>();
            for (int i = 0; i < totalKeys.length; i++) {
                ks.add(totalKeys[i]);
            }
            for (int i = 0; i < keys.length; i++) {
                int k = (int) (ks.size() * Math.random());
                keys[i] = ks.remove(k);
            }
            return keys;
        }
        return  new String[]{ "QQ", "1", "2", "3", "4",
                "5", "6", "7", "8", "9", "10","11","12","13","MSN" };
    }

    private static final int MSG_START_LOAD = 0x0102;
    private static final int MSG_END_LOAD   = 0x0203;

    private LoadKeywordsTask task = null;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
            case MSG_START_LOAD:
                    task = new LoadKeywordsTask();
                    new Thread(task).start();
                break;
            case MSG_END_LOAD:
                showKeywords.rubKeywords();
                feedKeywordsFlow(showKeywords, key_words);
                showKeywords.go2Show(KeywordsView.ANIMATION_IN);
                break;
            }

        }
    };

    private class LoadKeywordsTask implements Runnable{
        @Override
        public void run() {
            try {
                key_words = getRandomArray();
                if(key_words.length>0)
                    handler.sendEmptyMessage(MSG_END_LOAD);
            } catch (Exception e) {
            }
        }
    }

    private void feedKeywordsFlow(KeywordsView keyworldFlow, String[] arr) {
        for (int i = 0; i < KeywordsView.MAX; i++) {
            String tmp = arr[i];
            keyworldFlow.feedKeyword(tmp);
        }
    }

    class Mygdlinseter implements OnGestureListener {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public void onShowPress(MotionEvent e) {
        }
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                float distanceY) {
            return false;
        }
        @Override
        public void onLongPress(MotionEvent e) {
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            if (e2.getX() - e1.getX() > 100) {
                key_words = getRandomArray();
                showKeywords.rubKeywords();
                feedKeywordsFlow(showKeywords, key_words);
                showKeywords.go2Show(KeywordsView.ANIMATION_OUT);
                return true;
            }
            if (e2.getX() - e1.getX() < -100) {
                key_words = getRandomArray();
                showKeywords.rubKeywords();
                feedKeywordsFlow(showKeywords, key_words);
                showKeywords.go2Show(KeywordsView.ANIMATION_IN);
                return true;
            }
            if (e2.getY() - e1.getY() < -100) {
                key_words = getRandomArray();
                showKeywords.rubKeywords();
                feedKeywordsFlow(showKeywords, key_words);
                showKeywords.go2Show(KeywordsView.ANIMATION_IN);
                return true;
            }
            if (e2.getY() - e1.getY() > 100) {
                key_words = getRandomArray();
                showKeywords.rubKeywords();
                feedKeywordsFlow(showKeywords, key_words);
                showKeywords.go2Show(KeywordsView.ANIMATION_OUT);
                return true;
            }
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(isOutter){
            isOutter = false;

            String kw = ((TextView) v).getText().toString();
            Log.i(TAG, "keywords = "+kw);
            if (!kw.trim().equals("")) {
                searchLayout.removeAllViews();
            }
        }

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        switch(keyCode){
        case KeyEvent.KEYCODE_BACK:

            if(!isOutter){
                isOutter = true;
                searchLayout.removeAllViews();
                searchLayout.addView(showKeywords);
            }else{
                SearchPage.this.finish();
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
